﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using _MetalSlug.Form;

namespace _MetalSlug.Form.MenuScreen
{
    public class MyButton : VisibleGameEntity
    {
        MyScreen.TYPE forScreen;
        public MyScreen.TYPE ForScreen
        {
            get { return forScreen; }
            set { forScreen = value; }
        }

        public enum STATE
        {
            ENABLE,
            HOVER,
            DISABLE
        }

        STATE currentState;
        public STATE CurrentState
        {
            get { return currentState; }
            set 
            { 
                currentState = value;
                Alpha = 0;
                switch(CurrentState)
                {
                    case STATE.HOVER:
                        CurrentTexture = SelectedTexture;
                        break;

                    case STATE.ENABLE:
                        CurrentTexture = DefaultTexture;
                        break;

                    case STATE.DISABLE:
                        CurrentTexture = DisableTexture;
                        break;
                }
                
            }
        }

        Texture2D CurrentTexture;
        Texture2D SelectedTexture;
        Texture2D DefaultTexture;
        Texture2D DisableTexture;
        float Alpha;

        public MyButton(XmlElement xmlElement)
        {
            Alpha = 0;
            XmlNodeList Textures = xmlElement.SelectNodes("textures/Selected | textures/Default | textures/Disable");
            XmlElement X = (XmlElement)xmlElement.SelectSingleNode("x");
            XmlElement Y = (XmlElement)xmlElement.SelectSingleNode("y");
            XmlElement width = (XmlElement)xmlElement.SelectSingleNode("width");
            XmlElement height = (XmlElement)xmlElement.SelectSingleNode("height");
            foreach (XmlElement element in Textures)
            {
                switch(element.Name)
                {
                    case "Selected":
                        SelectedTexture = GameUtil.LoadTexture(element.InnerText);
                        break;

                    case "Default":
                        DefaultTexture = GameUtil.LoadTexture(element.InnerText);
                        break;

                    case "Disable":
                        DisableTexture = GameUtil.LoadTexture(element.InnerText);
                        break;
                }
            }

            TopLeft = new Vector2(float.Parse(X.InnerText),float.Parse(Y.InnerText));
            Size = new Vector2(float.Parse(width.InnerText), float.Parse(height.InnerText));
            CurrentState = STATE.ENABLE;
        }

        public override void Update(GameTime gameTime)
        {
            Alpha = MathHelper.Clamp(Alpha -= 0.01f, 0, 1);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(CurrentTexture,
                             new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)Size.X, (int)Size.Y), 
                             Color.Lerp(Color.White, Color.Transparent, Alpha));
        }

    }
}
